#include"Render/Renderer.hpp"
#include"Render/nuklear.h"

#ifdef LINUX
#include <unistd.h>  
#endif

int main()
{
    MyWindow window(1600,1200,"一路同型");

#ifdef IMGUI_USE
    // Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
// io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;         // IF using Docking Branch

// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window.GetWindow(), true);          // Second param install_callback=true will install GLFW callbacks and chain to existing ones.
ImGui_ImplOpenGL3_Init();
#endif
    

    // build and compile our shader program
    // ------------------------------------
    // vertex shader
    Shader sh("/home/gallon/code_git/code/一路同型/Shader/v.shader","/home/gallon/code_git/code/一路同型/Shader/f.shader");
    
    // set up vertex data (and buffer(s)) and configure vertex attributes
    // ------------------------------------------------------------------
    float vertices[] = {
        0.0f, 0.0f, 0.0f, 0.0f,0.0f,// left  BO
         1600.0f, 0.0f, 0.0f, 1.0f,0.0f,// right BO
         1600.0f,  1200.0f, 0.0f, 1.0f,1.0f, // R    
         0.0f,1200.0f,0.0f,    0.0f,1.0f//L    
    }; 
    
    unsigned int idex[]=
    {0,1,3,
         1,2,3
    };

    glm::mat4 prjo=glm::ortho(0.0f,1600.0f,0.0f,1200.0f,-1.0f,1.0f);
    
    //if blend
    // glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    // glEnable(GL_BLEND);
    VertexArray va;
    VertexBufferLayout layout;
    layout.Push(3, FLOAT);
    layout.Push(2, FLOAT);
    VertexBuffer vb(vertices,sizeof(vertices));
    IndexBuffer ib(idex,6);
    va.AddBuffer(vb, layout);
   
   std::string path("/home/gallon/code_git/code/一路同型/resourse/6.png");
   Texture texture(path);
   texture.Bind(0);
//    sh.setInt("u_Texture", 0);

    Renderer render;
    // uncomment this call to draw in wireframe polygons.
    // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    float val=0.0f;

#ifdef IMGUI_USE
char textbox[50]="LOVE";
#endif

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window.GetWindow()))
    {

#ifdef IMGUI_USE
        // Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow(); // Show demo window! :)

ImGui::Begin("imguiWindow");
ImGui::Text("lalala");
if(ImGui::Button("imguiButton"))
{
    std::cout<<"hello,imgui\n";
}
ImGui::InputText("text box", textbox, IM_ARRAYSIZE(textbox));
ImGui::End();
#endif


#ifdef NuKlear
enum {EASY, HARD};
static int op = EASY;
static float value = 0.6f;
static int i =  20;
struct nk_context ctx;
nk_init_fixed(&ctx, calloc(1, 1024), 1024, &font);
if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
    NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
    // fixed widget pixel width
    nk_layout_row_static(&ctx, 30, 80, 1);
    if (nk_button_label(&ctx, "button")) {
        // event handling
    }
    // fixed widget window ratio width
    nk_layout_row_dynamic(&ctx, 30, 2);
    if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
    if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
    // custom widget pixel width
    nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
    {
        nk_layout_row_push(&ctx, 50);
        nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
        nk_layout_row_push(&ctx, 110);
        nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
    }
    nk_layout_row_end(&ctx);
}
nk_end(&ctx);
#endif
        // input
        // -----
        processInput(window.GetWindow());

        // render
        // ------
        render.Clear();
        // draw our first triangle
        sh.use();
         sh.setMat4f("u_MVP", prjo);
        render.Draw(va, ib, sh);

        #ifdef LINUX
        // usleep(1*1000000);
        #endif

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // -------------------------------------------------------------------------------

        // Rendering

#ifdef IMGUI_USE
// (Your code clears your framebuffer, renders your other stuff etc.)
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
#endif

        glfwSwapBuffers(window.GetWindow());
        glfwPollEvents();
    }

    // optional: de-allocate all resources once they've outlived their purpose:
    // ------------------------------------------------------------------------
    va.UnBind();
    vb.UnBind();
    ib.UnBind();

#ifdef IMGUI_USE
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
#endif

    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------------------------
    glfwTerminate();
    return 0;
}

